"""
Django 响应
"""

import urllib.parse

from django.http import HttpResponse

import utils.enum_utils as enum


def get_response_for_download(
    file_read: bytes,
    file_name: str,
) -> HttpResponse:
    """
    获取用于下载的 HttpResponse

    Args:
        file_read: 文件内容
        file_name: 文件名

    Returns:
        响应
    """

    response: HttpResponse = HttpResponse(file_read, content_type=enum.ContentType.BINARY.value)
    quote_file_name: str = urllib.parse.quote(file_name)
    response["Content-Disposition"] = ("attachment; filename={0}; filename*=utf-8''{0}".format(quote_file_name))

    return response


def get_response_for_view(
    file_read: bytes,
    file_name: str,
    content_type: enum.ContentType.value_type,
) -> HttpResponse:
    """
    获取用于浏览器显示的 HttpResponse

    Args:
        file_read: 文件内容
        file_name: 文件名
        content_type: 响应体的 Content-Type

    Returns:
        响应
    """

    response: HttpResponse = HttpResponse(file_read, content_type=content_type)
    quote_file_name: str = urllib.parse.quote(file_name)
    response["Content-Disposition"] = ("inline; filename={0}; filename*=utf-8''{0}".format(quote_file_name))

    return response
